<?php
	/**
	* Products Management 
	* @author trungdt
	* @since Sep 1, 2011
	* 
	* Change logs
	* 
	*/

	class productsmodule {
		function name() { return "Products Module"; }
		function description() { return "Products Management"; }
		function author() { return "ABSoft.vn"; }

		function hasSources() { return true; }
		function hasContent() { return true; }
		function hasViews() { return true; }

		function supportsWorkflow() { return false; }

		function permissions($internal = "") {
			return array(
			'administrate'=>'Administrate',
			'configure'=>'Configure',
			'create'=>'Add new product',
			'edit'=>'Edit product',
			'delete'=>'Delete product',
			// for permissions of bills - carts
			'bill_management'=>'Bill manager',
			);
		}

		function show($view,$loc = null, $title = "") {
			// this module can only be viewed by those who have special permission
			if (!(exponent_users_isLoggedIn() && 
			(exponent_permissions_check("administrate", $loc) || exponent_permissions_check("configure", $loc) || exponent_permissions_check("create", $loc)  || exponent_permissions_check("edit", $loc) || exponent_permissions_check("delete", $loc))))
			{
				// return nothing if this user does not have any permission
				return;
			}
			global $db;

			$config = $db->selectObject('productsmodule_config',"location_data='".serialize($loc)."'");
			if ($config == null) {
				// not configured? Must be configure first
				$params=array();
				$params['module']=$loc->mod;
				$params['action']='configure';
				$params['src']=$loc->src;
				$params['_common']=1;
				$link=exponent_core_makeLink($params);
				echo "This module is not configured. <a href='{$link}'>Click here to configure this module</a>";
			}
			else
			{
				$template = new template('productsmodule',$view,$loc);
				$template->assign('config',$config);
				$template->register_permissions(array('administrate','configure','create','edit','delete'),$loc);
				$template->output();
			}
		}

		function deleteIn($loc) {
			global $db;
			$db->delete('productsmodule_product',"location_data='".serialize($loc)."'");
			$db->delete('productsmodule_config',"location_data='".serialize($loc)."'");
		}

		function copyContent($oloc,$nloc) {
			foreach ($db->selectObjects('productsmodule_products',"location_data='".serialize($oloc)."'") as $l) {
				$l->location_data = serialize($nloc);
				$db->insertObject($l,'productsmodule_products');
			}
		}

		function searchName() {
			return "Products";
		}

		/*
		8/29/2009 immanuel192
		This function create HTML content of spider item
		*/
		function spiderTemplate($album)
		{
			global $db;


			// create template content
			$template=new template("linklistmodule","_search");
			$template->assign("album",$album);
			ob_start();
			$template->output();
			$get_value=ob_get_contents();
			ob_end_clean(); 
			return $get_value;
		}

		/**
		* This function get all the category list
		* @author trungdt
		* @since Sep 29, 2011
		*/
		static function __GetCategoryList()
		{
			global $db;

			$data=exponent_sessions_get("__PDoremiShop_CategorySeoNameBuild_Cats");
			if ($data == null)
			{
				// save for later use, because, we understand that we need to use this data more than one time
				$data=$db->selectObjects("productsmodule_category");
				// first, we must add this into cache, because, we need in in __PDoremiShop_CategorySeoNameBuild
				exponent_sessions_set("__PDoremiShop_CategorySeoNameBuild_Cats", $data);
				// Oct 9, 2011 - trungdt - Add prebuild the link property first
				foreach ($data as $k=>$v){

					// prebuild link
					$temp=unserialize($v->location_data);
					$params=array();
					$params['module']=$temp->mod;
					$params['action']='view';
					$params['name']=productsmodule::__PDoremiShop_CategorySeoNameBuild($v->id);
					$params['src']=$temp->src;
					$v->link=exponent_core_makeLink($params);

					// for image file
					if (isset($v->file_id) && ($v->file_id > 0))
					{
						$file = $db->selectObject('file', 'id='.$v->file_id);
						$v->file_path = PATH_RELATIVE.$file->directory.'/'.$file->filename;
					}
					else
						$v->file_path="";

				}
				exponent_sessions_set("__PDoremiShop_CategorySeoNameBuild_Cats", $data);
			}
			return $data;
		}

		/**
		* Get Category by ID
		* 
		* @param mixed $id
		* @author trungdt
		* @since OCt 5, 2011
		*/
		static function __GetCategoryByID($id){
			$data=productsmodule::__GetCategoryList();
			foreach ($data as $k=>$v)
				if ($v->id == $id)
					return $v;
				return null;
		}

		/**
		* Get Category by seo_name
		* 
		* @param mixed $id
		* @author trungdt
		* @since OCt 5, 2011
		*/
		static function __GetCategoryBySeoName($seo_name){
			$data=productsmodule::__GetCategoryList();
			foreach ($data as $k=>$v)
				if ($v->seo_name == $seo_name)
					return $v;
				return null;
		}

		/**
		* Get Category parent_id
		* 
		* @param mixed $parent_id
		* @author trungdt
		* @since OCt 5, 2011
		*/
		static function __GetCategoryByParentID($parent_id){
			$data=productsmodule::__GetCategoryList();

			$temp=array();
			foreach ($data as $k=>$v)
				if ($v->parent_id == $parent_id)
					$temp[$k]=clone $v;
				return $temp;
		}

		/**
		* Build the SQL condition for recursive filter all products
		* @author trungdt 
		* @since Oct 10, 2011
		*/
		static function __CategoriesBuildConditionRecursive(&$sql, $parent){
			$cat=productsmodule::__GetCategoryByID($parent);
			if ($sql != '')
				$sql.=" OR ";
			$sql.=" (cat_id = {$cat->id}) ";
			$cats=productsmodule::__GetCategoryByParentID($parent);
			foreach ($cats as $k=>$v)
				productsmodule::__CategoriesBuildConditionRecursive($sql, $v->id);
			return $sql;
		}

		/**
		* This function get all the brands
		* @author trungdt
		* @since Sep 29, 2011
		*/
		static function __GetBrandList()
		{
			global $db;

			$data=exponent_sessions_get("__PDoremiShop_BrandSeoNameBuild_Cats");
			if ($data == null)
			{
				// save for later use, because, we understand that we need to use this data more than one time
				$data=$db->selectObjects("productsmodule_brand");
				exponent_sessions_set("__PDoremiShop_BrandSeoNameBuild_Cats", $data);
				foreach ($data as $k=>$v)
				{
					// for image file
					if (isset($v->logo_id) && ($v->logo_id > 0))
					{
						$file = $db->selectObject('file', 'id='.$v->logo_id);
						$v->logo_path = PATH_RELATIVE.$file->directory.'/'.$file->filename;
					}
					else
						$v->logo_path="";
				}
				exponent_sessions_set("__PDoremiShop_BrandSeoNameBuild_Cats", $data);
			}
			return $data;
		}

		/**
		* Get Brand by ID
		* 
		* @param mixed $id
		* @author trungdt
		* @since OCt 5, 2011
		*/
		static function __GetBrandByID($id){
			$data=productsmodule::__GetBrandList();
			foreach ($data as $k=>$v)
				if ($v->id == $id)
					return $v;
				return null;
		}

		/**
		* Get Brand by seo_name
		* 
		* @param mixed $id
		* @author trungdt
		* @since OCt 5, 2011
		*/
		static function __GetBrandBySeoName($seo_name){
			$data=productsmodule::__GetBrandList();
			foreach ($data as $k=>$v)
				if ($v->seo_name == $seo_name)
					return $v;
				return null;
		}

		/**
		* This function get the first category ID
		* Just use in DataModeler
		* 
		* @author trungdt
		* @since Sep 29
		*/
		static function GetFirstCategoryId()
		{
			$data=productsmodule::__GetCategoryList();
			if (count($data) > 0)
			{
				$item=array_shift($data);
				return $item->id;
			}
			else
			{
				return 0;
			}
		}

		/**
		* Category Section Seo Name Buid 
		* For the menu seo name link generate
		* Format type: cata|catb|catc
		* 
		* @author trungdt
		* @since Sep 28. 2011
		* @param mixed $cat_id
		* @param int $return_type: =0 (default) return string seo_name; =1: return array
		* @return string
		*/
		static function __PDoremiShop_CategorySeoNameBuild($cat_id, $return_type=0){
			$data=productsmodule::__GetCategoryList();

			$seo_ret="";
			$data_ret=array();
			$temp_cat=null;
			while (count($data)>0){
				$flag_exit=true;
				foreach ($data as $k=>$v)
					if ($v->id == $cat_id)
					{
						$seo_ret=$v->seo_name."|".$seo_ret;
						if ($return_type == 1)
							array_unshift($data_ret, clone $v);
						//
						$temp_cat=$v;
						$cat_id=$v->parent_id;
						unset($data[$k]);
						$flag_exit=false;
						break;
					}
					if ((isset($temp_cat->parent_id) && ($temp_cat->parent_id == 0)) || (count($data)==0) || $flag_exit)
					break;
			};
			if (strlen($seo_ret)>0 && substr($seo_ret,-1) == "|")
				$seo_ret=substr($seo_ret,0,-1);

			if ($return_type == 1)
				return $data_ret;
			else
				return $seo_ret;
		}

		/**
		* This function will automatic generate object for nagavition views 
		* Use with __PDoremiShop_GenrateMenu
		* 
		* @param mixed $cat Products Category
		* @author trungdt
		* @since Sep 27, 2011
		*/
		static function __PDoremiShop_GenerateMenuObject($cat, $depth){
			// id, name, sef_name, depth, link, 
			$obj=null;
			$obj->id=$cat->id*rand(1, 100);
			$obj->name=$cat->title;
			$obj->sef_name=$cat->seo_name;
			$obj->active=1;
			$temp=unserialize($cat->location_data);
			$params['module']=$temp->mod;
			$params['action']='view';
			//$params['name']=$cat->seo_name;
			$params['name']=productsmodule::__PDoremiShop_CategorySeoNameBuild($cat->id);
			$params['src']=$temp->src;
			$obj->link=exponent_core_makeLink($params);
			$obj->depth=$depth;
			return $obj;
		}

		/**
		* This function automatic generate sub menu for selected host name
		* 
		* @param mixed $host_sef_name
		* @return mixed
		*/
		static function __PDoremiShop_GenrateMenu($host_sef_name)
		{
			global $db;

			// get all category at the level 1
			$deps=$db->selectAndJoinObjects(null,'section_seo_name',"productsmodule_category","productsmodule_config","location_data","location_data","generate_menu = 1 AND parent_id =0 AND section_seo_name = '{$host_sef_name}'", "location_data, rank");

			$menu=array();

			foreach ($deps as $k=>$data1){
				// get data at level 2
				$subs=$db->selectObjects("productsmodule_category", "parent_id = {$data1->id} ORDER BY rank");
				// add menu level 1
				$menu[$k]=productsmodule::__PDoremiShop_GenerateMenuObject($data1, 0);

				foreach ($subs as $k1=>$v1){
					$temp=productsmodule::__PDoremiShop_GenerateMenuObject($v1, 1);
					$menu[$temp->id]=$temp;
				}
			}
			return $menu;
		}

		/**
		* Get currency List
		* @author trungdt
		* @since Oct 14, 2011
		*/
		static function _GetCurrencyList(){
			global $db;

			$data=exponent_sessions_get("__ABSoft_Productsmodule_CurrencyList");
			if ($data == null)
			{
				// save for later use, because, we understand that we need to use this data more than one time
				$data=$db->selectObjects("productsmodule_currency");
				exponent_sessions_set("__ABSoft_Productsmodule_CurrencyList", $data);
			}
			return $data;
		}

		/**
		* Get default currency, 
		* This function will automatically determine, load the currency by parameter or by the currency locale
		* @author trungdt
		* @since Oct 14, 2011 
		* @param $force_use = 1: Force use the result, not change
		*/
		static function _GetCurrencyDefault($default_id, $force_use=0)
		{
			$data=productsmodule::_GetCurrencyList();
			$cur=null;
			foreach ($data as $v)
				if (($v->id == $default_id) || ($v->locale == $default_id))
				{
					$cur=$v;
					break;
				}
				// automatic change to the right currency if the result currency not the same with language localle
				if (($cur != null) && ($force_use == 0)){
				if ($cur->locale != LANG)
				{
					$cur=productsmodule::_GetCurrencyDefault(LANG, 1);
				}
			}
			return $cur;
		}

		/**
		* Format the $value by the currency object
		* 
		* @param mixed $currency
		* @param mixed $value
		* @return float
		* @author trungdt
		*/
		function _GetCurrencyFormat($currency, $value){
			if ($value == -1)
				return -1;
			if (!isset($currency->rate))
				return $value;
			$value=floatval($value)*($currency->rate);

			if ($currency->precision > -1)
				$value=round($value, $currency->precision);

			// format the number
			if ($currency->precision == -1)
				$currency->precision =0;
			$value=number_format($value, $currency->precision, $currency->decimal_point, $currency->dec_point);

			if($currency->sign_position == 1)
				$value=$currency->sign.$value;
			else if($currency->sign_position == 2)
					$value.=$currency->sign;

				return $value;
		}

		/**
		* Process the product array before show
		* 
		* @param mixed $product_list
		* @param mixed $price_range_list : return the price range list if availeble
		* @author trungdt
		* @since OCt 5
		*/
		static function __ProductProcess($product_list, $config, &$price_max, &$price_min, $dont_load_image=true){
			global $db;
			$lang=exponent_lang_loadFile("modules/productsmodule/common.php");

			$currency=productsmodule::_GetCurrencyDefault($config->currency_id);

			if (!is_array($product_list))
				$product_list=array($product_list);

			$brand_cache=array();
			$cat_cache=array();
			$brand=null; $cat=null;

			foreach ($product_list as $k=>$v)
			{
				// for brand
				if (array_key_exists($v->brand_id, $brand_cache))
					$brand=$brand_cache[$v->brand_id];
				else
				{
					$brand=productsmodule::__GetBrandByID($v->brand_id);
					if ($brand != null)
						$brand_cache[$v->brand_id]=$brand;
				}
				if ($brand)
					$v->brand_name=$brand->title;
				else
					$v->brand_name = "";

				// for cat
				if (array_key_exists($v->cat_id, $cat_cache))
					$cat=$cat_cache[$v->cat_id];
				else
				{
					$cat=productsmodule::__GetCategoryByID($v->cat_id);
					if ($cat != null)
						$cat_cache[$v->cat_id]=clone $cat;
				}

				if ($cat)
					$v->cat_name=$cat->title;
				else
					$v->cat_name = "";

				// post_date
				$v->post_date=date("d/m/Y H:i", $v->post_date);

				// product state
				$v->product_state_text=$lang["product_state{$v->product_state}"];
				$v->product_state2_text=$lang["product_state_2_{$v->product_state2}"];

				// for price list
				if ($price_min == 0 || $price_min > $v->price)
					$price_min = $v->price;
				if ($price_max < $v->price)
					$price_max = $v->price;

				// for product price
				// back up all price before formating it
				$v->price_sale_bk = $v->price_sale;
				$v->price_bk = $v->price;
				if ($v->on_sale)
					$v->price_save_bk = $v->price - $v->price_sale;
				else
					$v->price_save_bk =0;
				$v->price_orginal_bk = $v->price_orginal;

				// price_save
				if ($v->on_sale == 1)
				{
					$v->price_save = productsmodule::_GetCurrencyFormat($currency, $v->price - $v->price_sale);
					$v->price_save_percent = round(floatval($v->price - $v->price_sale)/$v->price*100,2)."%";
				}
				else
				{
					$v->price_save = "";
					$v->price_save_percent = "";
				}
				$v->price=productsmodule::_GetCurrencyFormat($currency, $v->price);
				$v->price_orginal=productsmodule::_GetCurrencyFormat($currency, $v->price_orginal);
				$v->price_sale=productsmodule::_GetCurrencyFormat($currency, $v->price_sale); 

				// hot product, on_sale, best seller, for compatible with product edit data grid
				// this is for product management page only
				if ($dont_load_image)
				{
					$v->hot_product_checkbox="<input type=checkbox disabled ".(($v->hot_product==1)?"checked":"").">";
					$v->best_seller_checkbox="<input type=checkbox disabled ".(($v->best_seller==1)?"checked":"").">";
					$v->on_sale_checkbox="<input type=checkbox disabled ".(($v->on_sale==1)?"checked":"").">";
					$v->public_state_checkbox="<input type=checkbox disabled ".(($v->public_state==1)?"checked":"").">";
				}

				//Get the image file if there is one.
				if (isset($v->og_thumbnail) && ($v->og_thumbnail > 0)) {
					$file = $db->selectObject('file', 'id='.$v->og_thumbnail);
					$v->og_thumbnail_path = PATH_RELATIVE.$file->directory.'/'.$file->filename;
				}
				else
					$v->og_thumbnail_path=PATH_RELATIVE."files/noImage.gif";

				// load image list
				if (!$dont_load_image)
				{
					// load image list
					$temp=explode(";", $v->images);
					$v->image_list=array();
					foreach ($temp as $timage)
						if ($timage != "")
						{
							$file = $db->selectObject('file', 'id='.$timage);
							$timage = PATH_RELATIVE.$file->directory.'/'.$file->filename;
							$v->image_list[]=$timage;
						}
						$v->image_list_count=count($v->image_list);
					// load relative video and products
					$videos_temp=explode(";",$v->videos);
					$video_sql_where="";
					foreach ($videos_temp as $vk=>$vv)
						if ($vv!="")
						{
							if ($video_sql_where!= "")
								$video_sql_where.=" OR ";
							$video_sql_where.="(id = {$vv})";
						}
					if ($video_sql_where != "")
					{
						$videos=$db->selectObjects("productsmodule_video", $video_sql_where);
						$videos=productsmodule::__VideoProcess($videos);
					}
					else
						$videos=array();
					
					$v->videos_list=$videos;
					$v->videos_list_count=count($videos);
					// relative products
				}
			}
			return $product_list;
		}

		/**
		* Return the user Shipping 
		*/
		static function __Get_User_Shipping(&$info1, &$info2){
			global $db;
			global $user;

			$user_info=null;
			if (exponent_users_isLoggedIn())
			{
				$user_info = $db->selectObject("productsmodule_customerinfo", "user_id = {$user->id}");
				if ($user_info == null)
				{
					// add new info of this user into db
					$user_info->info1_name=$user->firstname." ".$user->lastname;
					$user_info->info1_address=$user->user_address->address1. " ".$user->user_address->address2;
					$user_info->info1_province=$user->user_address->city;
					$user_info->info1_city=$user->user_address->country;
					$user_info->info1_phone=$user->user_address->denomination;
					$user_info->info1_note="";

					//
					$user_info->info2_name="";
					$user_info->info2_address="";
					$user_info->info2_province="";
					$user_info->info2_city="";
					$user_info->info2_phone="";
					$user_info->info2_note="";

				}
			}
			if ($user_info == null)
			{
				$info1->info1_name=""; $info1->info1_address="";
				$info1->info1_province=""; $info1->info1_city="";
				$info1->info1_phone=""; $info1->info1_note="";

				$info2->info2_name=""; $info2->info2_address="";
				$info2->info2_province=""; $info2->info2_city="";
				$info2->info2_phone=""; $info2->info2_note="";
			}
			else
			{
				$info1->info1_name=$user_info->info1_name;
				$info1->info1_address=$user_info->info1_address;
				$info1->info1_province=$user_info->info1_province;
				$info1->info1_city=$user_info->info1_city;
				$info1->info1_phone=$user_info->info1_phone;
				$info1->info1_note=$user_info->info1_note;

				$info2->info2_name=$user_info->info2_name;
				$info2->info2_address=$user_info->info2_address;
				$info2->info2_province=$user_info->info2_province;
				$info2->info2_city=$user_info->info2_city;
				$info2->info2_phone=$user_info->info2_phone;
				$info2->info2_note=$user_info->info2_note;
			}
			return $user_info;
		}

		/**
		* Add new Bill Object and return default object
		* 
		*/
		static function __Bill_AddNew(){
			global $db;

			// new bill object
			$bill=null;
			$bill->buyer_user_id=0;
			$bill->buyer_user_name="";
			$bill->bill_total=0;
			$bill->bill_state=0;
			$bill->buy_date=0;
			// for shipping info
			$bill->info1_name="";
			$bill->info1_address=0;
			$bill->info1_province=0;
			$bill->info1_city=0;
			$bill->info1_phone=0;
			$bill->info1_note=0;
			//
			$bill->info2_name="";
			$bill->info2_address=0;
			$bill->info2_province=0;
			$bill->info2_city=0;
			$bill->info2_phone=0;
			$bill->info2_note=0;
			//
			$bill->included_tax=0;
			$bill->included_shipping=0;
			$bill->quantity_count=0;
			$bill->ip_address="";

			$bill->id=$db->insertObject($bill, "productsmodule_bills");
			return $bill;
		}

		/**
		* Add logs into bill
		* @author trungdt
		* @since oct 21, 2011
		* @param mixed $content
		* @param $bill_id : =-1: Bill ID; =-1: System Logs
		*/
		static function __Bill_AddLogs($content, $bill_id=-1){
			if (!defined('SYS_USERS')) include_once(BASE.'subsystems/users.php');
			global $db;
			global $user;

			if ($bill_id < 0)
			{
				$content.="Request made at ".date('F j, Y, H:i:s',aboid_getCurrentTime()). " by ip address ".$_SERVER['REMOTE_ADDR'];
			}

			$temp=null;
			$temp->bill_id=$bill_id;
			$temp->post_date = aboid_getCurrentTime();
			$temp->changer_username=$user->username;
			$temp->changer_fullname = $user->firstname." ".$user->lastname;
			$temp->log_content = $content;
			$db->insertObject($temp, "productsmodule_bills_logs");
		}

		/**
		* This function send mail to commnunicate with admin and customer
		* Default: automatic send mail to admin
		* @author trungdt
		* @since Oct 25, 2011
		* 
		* @param mixed $subject
		* @param mixed $content
		* @param int $format: =0: plain text, =1 html 
		* @param mixed $receives
		* @param $dont_add_admin : dont include admin into received list
		*/
		static function SendMail($subject, $content, $format=0, $receive=array(), $dont_add_admin=false){
			// send email to admin 
			$headers['From'] =  SMTP_FROMADDRESS ;
			$headers['Reply-to'] = SMTP_FROMADDRESS;

			require_once(BASE.'subsystems/mail.php');
			$mail = null;
			$mail = new exponentMail();

			if ($dont_add_admin==false)
				$mail->addTo(SMTP_FROMADDRESS);
			if (!is_array($receive))
			{
				$receive=array($receive);
			}
			foreach ($receive as $v)
				$mail->addTo($v);

			$mail->subject($subject);
			if ($format == 0)
				$mail->addText($content);
			else
				$mail->addHTML($content);
			$mail->addHeaders($headers);
			$mail->addFrom(SMTP_FROMADDRESS, SITE_TITLE );
			try{
				@$mail->send();
			}
			catch (Exception $e){

			}
		}

		/**
		* This function generate price filter list
		* @return array Array of price list, formatted
		* @author trungdt
		* @since Oct 25, 2011
		* @param mixed $currency
		*/
		static function __Generate_Price_Filter_list($currency){
			// price list
			$price_list=array();
			$price_list[]=array(0,25);
			$price_list[]=array(25,50);
			$price_list[]=array(50,100);
			$price_list[]=array(100,200);
			$price_list[]=array(200,300);
			$price_list[]=array(300,500);
			$price_list[]=array(500,750);
			$price_list[]=array(750,1000);
			$price_list[]=array(1000,1500);
			$price_list[]=array(1500,2000);
			$price_list[]=array(2000,3000);
			$price_list[]=array(3000,5000);
			$price_list[]=array(5000,-1);
			foreach ($price_list as $k=>$v)
			{
				/** Hack this because we dont want to change $price_list */
				$v[0]*=20000;
				if ($v[1] != -1)
					$v[1]*=20000;
				/** Hack this because we dont want to change $price_list */
				$v1=productsmodule::_GetCurrencyFormat($currency, $v[0]);
				$v2=productsmodule::_GetCurrencyFormat($currency, $v[1]);
				$temp=null;
				$temp->p1=$v[0];
				$temp->p2=$v[1];
				$temp->v1=$v1;
				$temp->v2=$v2;
				$price_list[$k]=$temp;
			}
			return $price_list;
		}
		
		/**
		* This function process inputed videos
		* 
		* @param mixed $videos
		*/
		static function __VideoProcess($videos, $load_video=0){
			global $db;
			
			if (!is_array($videos))
				$videos=array($videos);
			
			$cache_cat=array();
			
			foreach ($videos as $k=>$v)
			{
				$v->title_in_lower=strtolower($v->title);
				
				if (isset($cache_cat[$v->cat_id]))
				{
					$v->cat_title=$cache_cat[$v->cat_id]->title;       
					$v->cat_seo=$cache_cat[$v->cat_id]->seo_name;       
				}
				else
				{
					$cat=$db->selectObject("productsmodule_video_cat", "id = {$v->cat_id}");
					$v->cat_title=$cat->title;
					$v->cat_seo=$cat->seo_name;
					$cache_cat[$cat->id]=$cat;
				}
				
				if ($load_video == 1)
				{
					if ($v->video_type == 2)
					{
						$v->file_path="http://www.youtube.com/watch?v=".$v->youtube_embed;
					}
					else
					{
						$file=$db->selectObject("file", "id = {$v->file_id}");
						if ($file)
							$v->file_path=URL_FULL.$file->directory."/".$file->filename;
						else
							$v->file_path="";
					}
				}
				if (($v->video_type == 2 && $v->use_own_thumbnail == 1) || ($v->video_type == 0)){
					//
					$file=$db->selectObject("file", "id = {$v->thumbnail}");
					if ($file)
						$v->thumbnail=URL_FULL.$file->directory."/".$file->filename;
					else
						$v->thumbnail="";
				}
				else if ($v->video_type == 2){
					// generate youtube thumbnail
					$v->thumbnail="http://img.youtube.com/vi/{$v->youtube_embed}/0.jpg";
				}
				else if ($v->video_type == 1)
						$v->thumbnail=URL_FULL.THEME_RELATIVE."modules/productsmodule/images/mp3.png";
					//
				
				
				// url backlink
				$v->url_link=exponent_core_makeLink(array(
					"module"=>"productsmodule",
					"action"=>"_video_main",
					"src"=>unserialize($v->location_data)->src,
					"cat_name"=>$v->cat_seo,
					"video_name"=>$v->seo_name
				))."#".$v->seo_name;
			}
			return $videos;
		}
	}
?>
